import wx
class ImagePanel(wx.Panel):
    def __init__(self, *args, **kwds):
        wx.Panel.__init__(self, *args, **kwds)
        self.image = None
        self.Bind(wx.EVT_PAINT, self.__fit_image__)
    def setImage(self, imageFile):
        self.image = wx.Image(imageFile, wx.BITMAP_TYPE_ANY)
        self.__fit_image__(None)
    def __fit_image__(self, event):
        if self.image != None:
            dc = wx.ClientDC(self)
            rect = self.GetUpdateRegion().GetBox()
            dc.SetClippingRect(rect)
            width = self.image.GetWidth()
            height = self.image.GetHeight()
            if self.GetSize().GetWidth() < width:
                height = int(height * float(self.GetSize().GetWidth()) / width)
                width = self.GetSize().GetWidth()
            if self.GetSize().GetHeight() < height:
                width = int(width * float(self.GetSize().GetHeight()) / height)
                height = self.GetSize().GetHeight()
            dc.Clear()
            dc.DrawBitmap(self.image.Scale(width, height, wx.IMAGE_QUALITY_HIGH).ConvertToBitmap(), (self.GetSize().GetWidth() - width) / 2, (self.GetSize().GetHeight() - height) / 2)
            self.Layout()